package com.asa.ui.B;

import java.awt.EventQueue;
import java.awt.Point;

import com.asa.ui.B.AlgoFrame;
import com.asa.ui.frame.AlgoVisHelper;

public class AlgoVisualizer {
	private AlgoFrame frame;
	private MonteCarloData data;
	private int Number;
	private int screenWidth;
	public AlgoVisualizer(final int screenWidth,final int screenHeight,int Number){
		if( screenWidth!=screenHeight){
			throw new IllegalArgumentException("输入不符合要求");
		}
		//实例化圆
		Circle circle=new Circle(screenWidth/2, screenWidth/2,screenWidth/2);
		data=new MonteCarloData(circle);
		this.Number=Number;
		this.screenWidth=screenWidth;
		//初始化视图
//		EventQueue.invokeLater(()->{
//			frame=new AlgoFrame("蒙特卡罗算法求PI",screenWidth,screenHeight);
//			new Thread(()->run()).start();
//		});
		
		
		EventQueue.invokeLater(new Runnable() {//用java事件分发（开了个线程）的方法来创建对话框
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				frame=new AlgoFrame("蒙特卡罗算法求PI",screenWidth,screenHeight);
				
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						runmiage();
					}

					
				}).start(); ;
				
			}
		});
		
		
		
	}
	//播放动画
	public void runmiage(){
			for(int i=0;i<Number;i++){
				//每生成100个点渲染一次
				if(i%100==0){
					//绘制数据
					frame.render(data);
					//线程休眠20ms
					AlgoVisHelper.pause(10);
					System.out.println(data.questPI());
				}
				    //随机生成点的坐标
					int x=(int)(Math.random()*screenWidth);
					int y=(int)(Math.random()*screenWidth);
					data.addPoint(new Point(x,y));
					//如果点在圆内则圆内点数量++
					data.countInsideCircle(i);
				
			}
	}
}

